package ManagerImage.entity;
import ManagerImage.entity.SlideShow;
import ManagerImage.Utils.FileUtil;
import ManagerImage.Utils.StatusUtil;

import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import java.io.File;

import static ManagerImage.constant.ViewConst.THUMBNAIL_HEIGHT;
import static ManagerImage.constant.ViewConst.THUMBNAIL_WIDTH;
import static ManagerImage.display.RootPane.*;

/**
 * ⽤于显示⼀个图像的类
 * from RootPane:
 * 所需变量->selectedImages  currentPath statusLabel totalSize imageCount
 * 所需函数->clearSelectedThumbnails();
 */
public class Thumbnail extends BorderPane {
    private  File imageFile;
    private Label label; // 标签用于显示文件名
    private Canvas canvas; // 画布用于显示图像
    //tool
    StatusUtil statusUtil=new StatusUtil();
    FileUtil fileUtil=new FileUtil();
    public Thumbnail(File imageFile) {
        this.imageFile=imageFile;
        // 创建绘制图⽚的画布
        canvas = new Canvas(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT);
        GraphicsContext gc = canvas.getGraphicsContext2D(); // 获得画笔对象
        loadImage(gc);
        label = new Label(imageFile.getName());
        label.prefWidthProperty().bind(canvas.widthProperty());
        label.setAlignment(Pos.CENTER);
        this.setCenter(canvas);
        this.setBottom(label);
        this.setMaxWidth(THUMBNAIL_WIDTH + 10);
        this.getStyleClass().add("thumbnail-default");
        this.setOnMouseEntered(event -> this.getStyleClass().add("thumbnail-hover"));
        this.setOnMouseExited(event -> this.getStyleClass().remove("thumbnail-hover"));
        //双击幻灯片
        this.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> handleThumbnailMouseClicked(event, imageFile));
        //左键选中机制
        this.setOnMousePressed(event -> handleThumbnailMousePressed(event,imageFile));
    }
    /**
     * // 读取图⽚⽂件，按照缩略图⼤⼩读取，并保持宽⾼⽐例
     * @param gc
     */
    private void loadImage(GraphicsContext gc) {
        Image image = new Image(imageFile.toURI().toString(),
                THUMBNAIL_WIDTH - 2,
                THUMBNAIL_HEIGHT - 2, true, true);
        // 计算绘制图⽚的坐标
        double x = (THUMBNAIL_WIDTH - image.getWidth()) / 2;
        double y = (THUMBNAIL_HEIGHT - image.getHeight()) / 2;
        gc.clearRect(0, 0, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT); // 清除画布
        gc.drawImage(image, x, y); // 绘制图⽚到Canvas
    }
    private void handleThumbnailMouseClicked(MouseEvent event, File imageFile) {
        if(imageFile==null)return;
        if (event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 2) {
            new SlideShow(imageFile); // 双击打开幻灯片播放
        }
    }
    private void handleThumbnailMousePressed(MouseEvent event,File imageFile) {
        if (event.isPrimaryButtonDown()) { // 左键选中
            boolean ctrlPressed = event.isControlDown();
            System.out.println("左键按下");
            if (!ctrlPressed) {
                System.out.println("清除之前的选中状态");
                // 清除之前选中的缩略图
                clearSelectedThumbnails();
            }
            // 处理选中或取消选中
            if (selectedImages.contains(imageFile)) {
                // 如果已选中，则取消选中
                selectedImages.remove(imageFile);
                System.out.println("恢复默认样式");
                this.setStyle(""); // 恢复默认样式
            } else {
                // 如果没有选中，则添加到选中列表
                selectedImages.add(imageFile);
                System.out.println("选中");
                this.setStyle("-fx-border-color: #e1e10d; -fx-border-width: 2;"); // 设置选中样式
            }
            File currentDirectory = new File(currentPath);
            statusUtil.updateStatus(statusLabel, "加载完成: " + currentDirectory.getName() + " | 图片个数: " + imageCount + " | 总大小: " + fileUtil.formatSize(totalSize) + " |已选中: " + selectedImages.size() + " 张图片");
        }
    }
    public File getImageFile() {
        return imageFile;
    }
}

